<?php

namespace addons\sum\controller\store;

use addons\sum\controller\Common;
use addons\sum\service\store\Store as StroeService;
use app\admin\model\sum\store\StoreGoods as StoreGoodsModel;
use app\admin\model\User;

class Goods extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 商品列表
     * @return void
     */
    public function index()
    {
        $user = auth_user();
        if($user['store_status']!=2){
            $this->error('你还不是商户，暂无权限');
        }
        $store_id=$user['store_id']??'';
        if(!$store_id){
            $this->error('商户参数错误');
        }
        $params = $this->request->param();
        $category_id = $params['category_id'] ?? '';
        $sort = $params['sort'] ?? 'weigh';
        $order = $params['order'] ?? 'desc';
        $recodes = StoreGoodsModel::hasWhere('goods',function ($q) use($category_id){
            if ($category_id) {
                $q=$q->where('category_ids',$category_id);
            }
            $q=$q->where('Goods.status','up');
            return $q;
        })->with(['goods'])
            ->where('store_id',$store_id)
            ->order($sort, $order)
            ->paginate();

        $this->success('获取成功', $recodes);
    }

    /**
     * 商户编辑商品-商户价格
     * @return void
     */
    public function edit()
    {
        $user = auth_user();
        if($user['store_status']!=2){
            $this->error('你还不是商户，暂无权限');
        }
        $store_id=$user['store_id']??'';
        if(!$store_id){
            $this->error('商户参数错误');
        }
        $params = $this->request->param();
        $goods_id = $params['goods_id'] ?? '';
        if(!$goods_id){
            $this->error('商品参数错误');
        }
        $store_goods=StoreGoodsModel::where('goods_id',$goods_id)->where('store_id',$store_id)->find();
        if(!$store_goods){
            $this->error('商户商品参数错误');
        }
        $price=$params['price'] ?? '';
        if(!$price){
            $this->error('价格参数错误');
        }
        $store_goods->price=$price;
        $store_goods->save();
        $this->success('操作成功');
    }

}
